<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 刷新列表按钮 -->
    <el-button type="primary" class="refreshBtn" @click="refresh"
      >刷新</el-button
    >
    <!-- 卡片视图 -->
    <el-card class="user-card">
      <!-- 搜索区 -->
      <el-row>
        <el-col :span="8">
          <el-input
            v-model="queryInfo.goods_name"
            clearable
            @clear="getGoodsList"
            placeholder="请输入商品名称"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表单区 -->
      <el-table
        v-loading="loading"
        :data="goodsList"
        border
        stripe
        height="75vh"
        class="table"
      >
        <!-- 下拉 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-card class="elCard">
              <h2>编辑商品信息</h2>
              <el-form
                label-position="left"
                class="demo-table-expand"
                label-width="100px"
                :rules="editFormRules"
                :model="props.row"
                ref="editGoodsRef"
              >
                <el-form-item label="商品名称:" prop="goods_name">
                  <el-input v-model="props.row.goods_name"></el-input>
                </el-form-item>
                <el-form-item label="商品重量:">
                  <el-input-number
                    :min="1"
                    v-model.number="props.row.goods_weight"
                  ></el-input-number>
                </el-form-item>
                <el-form-item label="商品价格:">
                  <el-input-number
                    :min="0.01"
                    v-model.number="props.row.goods_price"
                  ></el-input-number>
                </el-form-item>
                <el-form-item label="商品数量:">
                  <el-input-number
                    :min="0"
                    v-model.number="props.row.goods_number"
                  ></el-input-number>
                </el-form-item>
                <el-form-item class="formBtn">
                  <el-button type="primary" @click="editGoodsInfo(props.row)"
                    >确认</el-button
                  >
                  <el-button type="info" @click="getGoodsList">取消</el-button>
                </el-form-item>
              </el-form>
            </el-card>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="800">
        </el-table-column>
        <el-table-column label="商品图片（点击预览）" width="200">
          <template slot-scope="props">
            <el-image
              style="width: 50px; height: 50px"
              :src="props.row.goods_big_logo"
              :preview-src-list="[props.row.goods_big_logo]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="delete_time" label="上下架状态" width="150">
          <template slot-scope="props">
            <el-tooltip
              :content="props.row.delete_time == '1' ? '已上架' : '未上架'"
              placement="top"
              :enterable="false"
            >
              <el-switch
                v-model="props.row.delete_time"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="changeStatus(props.row)"
              >
              </el-switch>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
            >
              <i class="el-icon-delete"></i>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页栏 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        goods_name: "",
        pagesize: 10,
        page: 1,
      },
      isrow: true,
      // 添加商品对话框的显示隐藏
      showAddGoods: false,
      // 商品列表数据
      goodsList: [],
      // 总数量
      total: 0,
      // 商品修改信息
      goodsEdit: {},
      // 商品修改表单验证规则
      editFormRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
      },
      // 加载的显示隐藏
      loading: false,
    };
  },
  created() {
    this.getGoodsList();
  },

  methods: {
    // 获取商品信息
    async getGoodsList() {
      this.loading = true;
      const { data: res } = await this.$http.get("/api/goods/getAll", {
        params: this.queryInfo,
        ...this.getToken,
      });
      console.log(res);
      if (res.code !== 1) return this.$message.error("获取商品列表失败！");
      this.loading = false;
      this.goodsList = res.data.list;
      this.total = res.data.total;
      console.log(this.goodsList);
    },
    // 刷新列表按钮
    refresh() {
      (this.queryInfo.goods_name = ""),
        (this.queryInfo.pagesize = 10),
        (this.queryInfo.page = 1);
      this.getGoodsList();
    },
    //商品查询按钮
    search() {
      this.queryInfo.page = 1;
      this.getGoodsList();
    },
    // 改变商品上下架状态
    async changeStatus(obj) {
      const { data: res } = await this.$http.put(
        "/api/goods/" + obj.goods_id,
        { delete_time: obj.delete_time },
        this.getToken
      );
      if (res.code !== 1) return this.$message.error("修改上下架状态失败！");
      this.$message.success("修改上下架状态成功！");
      console.log(res);
    },
    // 编辑商品信息
    editGoodsInfo(info) {
      // console.log(info);
      this.$refs.editGoodsRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "/api/goods/" + info.goods_id,
          info,
          this.getToken
        );
        if (res.code !== 1) return this.$message.error("修改商品信息失败！");
        this.$message.success("修改商品信息成功！");
      });
    },
    // 取消编辑
    cancelEdit() {
      this.$refs.editGoodsRef.resetFields();
    },
    // 监视条数改变
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getGoodsList();
    },
    // 监视页码改变
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getGoodsList();
    },
    // 删除商品
    async removeGoods(id) {
      console.log(id);
      const isremove = await this.$confirm(
        "此操作将永久删除该商品信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (isremove === "cancel") return this.$message.info("已取消删除！");
      const { data: res } = await this.$http.delete(
        "/api/goods/" + id,
        this.getToken
      );
      console.log(res);
      if (res.code !== 1) return this.$message.error("删除商品失败！");
      this.$message.success("删除商品成功！");
      this.getGoodsList();
      console.log(res);
    },
  },
  computed: {
    getToken() {
      return { headers: { authorization: sessionStorage.getItem("token") } };
    },
  },
};
</script>

<style lang="less" scoped>
.demo-table-expand {
  margin-top: 20px;
}
.formBtn {
  display: flex;
  justify-content: center;
}
</style>